﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
using System.Linq;
using VocabManager.Command;
using VocabManager.Model;
using VocabManager.View;

namespace VocabManager.ViewModel
{
    public class AppListViewModel : ViewModelBase
    {
        VocabManager.DataLayer.DeckRepository _deckRepository;
        private VocabManager.DataLayer.ItemRepository _itemRepository;

        private ObservableCollection<DeckViewModel> AllDecks { get; set; }
       
        CollectionViewSource _collectionViewSource;

        public ICollectionView Decks
        {
            get
            {
                return _collectionViewSource.View;
            }

        }
        public AppListViewModel(VocabManager.DataLayer.DeckRepository deckRepository,  
            VocabManager.DataLayer.ItemRepository itemRepository)
        {
            _itemRepository = itemRepository;
            _deckRepository = deckRepository;
            LoadItems();
        }

        private void LoadItems()
        {
          AllDecks = new ObservableCollection<DeckViewModel>( 
              _deckRepository.GetDeckList().Select(deck=> new DeckViewModel(this,_itemRepository,deck)));
          _collectionViewSource = new CollectionViewSource();
          _collectionViewSource.Source = AllDecks;
            Decks.CurrentChanged += new EventHandler(Decks_CurrentChanged);
        }

        void Decks_CurrentChanged(object sender, EventArgs e)
        {
            OnPropertyChanged("CurrentItem");
        }

        public string ApplicationName
        {
            get
            {
                return "Flash Card";
            }
        }
        public DeckViewModel CurrentDeck
        {
            get
            {
                return Decks.CurrentItem as DeckViewModel;
            }
        }


        #region Remove
        RelayCommand _removeCurrentDeckCommand;
        public ICommand RemoveCurrentDeckCommand
        {
            get
            {
                if (_removeCurrentDeckCommand == null)
                {
                    _removeCurrentDeckCommand = new RelayCommand(
                        param => this.RemoveCurrentDeck(),
                        param => this.CanRemoveCurrentDeck()
                        );
                }
                return _removeCurrentDeckCommand;
            }
        }

        private void RemoveCurrentDeck()
        {
            RemoveDeck(CurrentDeck);
        }


        private bool CanRemoveCurrentDeck()
        {
            return true;
        }
        public void RemoveDeck(DeckViewModel deck)
        {
            if (this.Decks.CurrentItem == deck)
                this.Decks.MoveCurrentToNext();
            this.AllDecks.Remove(deck);
            if (!deck.IsNew)
                _deckRepository.RemoveDeck(deck.Deck);



        }
        #endregion
        #region AddNew
        RelayCommand _addNewCommand;
        public ICommand AddNewCommand
        {
            get
            {
                if (_addNewCommand == null)
                {
                    _addNewCommand = new RelayCommand(
                        param => this.AddNew(),
                        param => this.CanAddNew()
                        );
                }
                return _addNewCommand;
            }
        }

        private bool CanAddNew()
        {

            throw new NotImplementedException();
        }

        private void AddNew()
        {
            Deck newDeck= Deck.CreateNew();
            DeckViewModel deckViewModel = new DeckViewModel(this,_itemRepository, newDeck);
             deckViewModel.IsNew = deckViewModel.IsInEditMode= true;

            AllDecks.Add(deckViewModel);
            Decks.MoveCurrentTo(deckViewModel);

            DeckViewEdit editView = new DeckViewEdit();
            editView.DataContext = deckViewModel;
            App.Instance.MainPage.LoadView(editView);

        }
        #endregion

        #region View
        RelayCommand _viewSelectedDeckCommand;
        public ICommand ViewSelectedDeckCommand
        {
            get
            {
                if (_viewSelectedDeckCommand == null)
                {
                    _viewSelectedDeckCommand = new RelayCommand(
                        param => this.ViewSelectedDeck(),
                        param => this.CanViewSelectedDeckCommand()
                        );
                }
                return _viewSelectedDeckCommand;
            }
        }

        private bool CanViewSelectedDeckCommand()
        {

            throw new NotImplementedException();
        }

        private void ViewSelectedDeck()
        {
            if(this.CurrentDeck != null)
             ViewDeck(this.CurrentDeck);

        }
        private void ViewDeck(DeckViewModel deckViewModel)
        {
            DeckView viewDeckView = new DeckView();
            deckViewModel.LoadItems();
            
            viewDeckView.DataContext = deckViewModel;
            App.Instance.MainPage.LoadView(viewDeckView);
        }
        #endregion

        internal void SaveDeck(DeckViewModel deck)
        {
            if (deck.IsNew)
                _deckRepository.AddNewDeck(deck.Deck);
            else
                _deckRepository.UpdateDeck(deck.Deck);
        }

  
    }
}
